﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using Util.Database;

namespace AutoNZBLoader
{
    public class Feed
    {
        private readonly string _Address;
        public string Address
        {
            get { return _Address; }
        }

        private readonly IList<FeedItem> _Items;
        public IList<FeedItem> Items
        {
            get { return _Items; }
        } 

        private Feed(string address)
        {
            this._Address = address;

            List<FeedItem> lst = new List<FeedItem>();
            byte[] rawFeed = null;
            int errorCount = 0;
            while (errorCount < 3)
            {
                try
                {
                    using (System.Net.WebClient Client = new System.Net.WebClient())
                    {
                        rawFeed = Client.DownloadData(address);
                        break;
                    }
                }
                catch (System.Net.WebException)
                {
                    errorCount++;
                }
            }
            if (errorCount == 3)
            {
                _Items = new System.Collections.ObjectModel.ReadOnlyCollection<FeedItem>(lst);
                return;
            }

            XmlDocument d = new XmlDocument();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(rawFeed))
            {
                d.Load(ms);
                XmlNodeList nodelist = d.GetElementsByTagName("item");
                foreach (XmlNode n in nodelist)
                {
                    string Title = "";
                    string Description = "";
                    string Link = "";
                    foreach (XmlNode c in n.ChildNodes)
                    {
                        if (c.Name.ToUpperInvariant() == "TITLE")
                        {
                            Title = c.InnerText;
                        }
                        else if (c.Name.ToUpperInvariant() == "LINK")
                        {
                            Link = c.InnerText;
                        }
                        else if (c.Name.ToUpperInvariant() == "DESCRIPTION")
                        {
                            Description = c.InnerText;
                        }
                    }

                    try
                    {
                        lst.Add(new FeedItem(Title, Description, Link));
                    }
                    catch (ArgumentException) { }
                }
            }

            _Items = new System.Collections.ObjectModel.ReadOnlyCollection<FeedItem>(lst);
        }


        private static IList<Feed> _Feeds;
        public static IList<Feed> Feeds
        {
            get
            {
                if (_Feeds == null)
                {
                    List<Feed> lst = new List<Feed>();

                    using (System.Data.IDbCommand cmd = DB.CreateCommand())
                    {
                        cmd.CommandText = "select * from feed";
                        using (IDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                string URL = dr["ADDRESS"].ToString();
                                lst.Add(new Feed(URL));
                            }
                        }
                    }
                    _Feeds = new System.Collections.ObjectModel.ReadOnlyCollection<Feed>(lst);
                }
                return _Feeds;
            }
        }
    }
}
